package com.codeiy.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.codeiy.criteria.CriteriaQuery;
import com.codeiy.domain.MetaModel;
import com.codeiy.domain.ObjectInstance;
import com.codeiy.dto.R;
import com.codeiy.entity.data.DataWarehouse;
import com.codeiy.service.DataWarehouseService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/warehouse")
public class DataWarehouseController {
    private final DataWarehouseService dataWarehouseService;
    private final MetaModel metaModel;

    /**
     * 通过ID查询
     *
     * @param id ID
     */
    @GetMapping("/{projectCode}/{objectCode}/{id:\\d+}")
    public R<Object> getById(@PathVariable String projectCode, @PathVariable String objectCode, @PathVariable Long id) {
        DataWarehouse data = dataWarehouseService.getById(id);
        Object object = metaModel.toModel(projectCode, objectCode, data);
        return R.ok(object);
    }

    /**
     * 查询对象信息
     *
     * @param criteriaQuery 查询条件
     * @return 对象信息
     */
    @PostMapping("/get")
    public R<Object> get(@RequestBody CriteriaQuery criteriaQuery) {
        String projectCode = criteriaQuery.getProjectCode();
        String objectCode = criteriaQuery.getObjectCode();
        QueryWrapper<DataWarehouse> queryWrapper = metaModel.getQueryWrapper(projectCode, objectCode, criteriaQuery);
        DataWarehouse data = dataWarehouseService.getOne(queryWrapper);
        if (data == null) {
            return R.ok();
        }
        return R.ok(metaModel.toModel(projectCode, objectCode, data));
    }

    /**
     * 查询分页信息
     *
     * @param criteriaQuery 查询对象
     * @return 分页对象
     */
    @PostMapping("/page")
    public R<IPage<Object>> page(@RequestBody CriteriaQuery criteriaQuery) {
        String projectCode = criteriaQuery.getProjectCode();
        String objectCode = criteriaQuery.getObjectCode();
        QueryWrapper<DataWarehouse> queryWrapper = metaModel.getQueryWrapper(projectCode, objectCode, criteriaQuery);
        Page<DataWarehouse> dataPage = dataWarehouseService.page(criteriaQuery.toMybatisPlusPage(), queryWrapper);
        Page<Object> objectPage = new Page<>(dataPage.getCurrent(), dataPage.getSize(), dataPage.getTotal());
        List<DataWarehouse> dataList = dataPage.getRecords();
        objectPage.setRecords(metaModel.toModelList(projectCode, objectCode, dataList));
        return R.ok(objectPage);
    }

    /**
     * 添加
     *
     * @param objectInstance 实体
     * @return success/false
     */
    @PostMapping
    public R<Boolean> save(@RequestBody ObjectInstance objectInstance) {
        String projectCode = objectInstance.getProjectCode();
        String objectCode = objectInstance.getObjectCode();
        DataWarehouse data = metaModel.toData(projectCode, objectCode, objectInstance.getBean());
        data.setTenantId(1L);
        data.setProjectCode(projectCode);
        data.setObjectCode(objectCode);
        return R.ok(dataWarehouseService.save(data));
    }

    /**
     * 删除
     *
     * @param id ID
     * @return success/false
     */
    @DeleteMapping("/{projectCode}/{objectCode}/{id:\\d+}")
    public R<Boolean> removeById(@PathVariable String projectCode, @PathVariable String objectCode, @PathVariable Long id) {
        dataWarehouseService.removeById(id);
        return R.ok();
    }

    /**
     * 编辑
     *
     * @param objectInstance 实体
     * @return success/false
     */
    @PutMapping
    public R<Boolean> update(@RequestBody ObjectInstance objectInstance) {
        Long id = objectInstance.getId();
        if (id == null) {
            return R.failed("id不能为空");
        }
        String projectCode = objectInstance.getProjectCode();
        String objectCode = objectInstance.getObjectCode();

        Object bean = objectInstance.getBean();
        DataWarehouse oldData = dataWarehouseService.getById(id);
        if (oldData == null) {
            return R.failed("根据主键信息查无数据");
        }
        DataWarehouse newData = metaModel.toData(projectCode, objectCode, bean);
        BeanUtil.copyProperties(newData, oldData, "dataId");
        return R.ok(dataWarehouseService.updateById(oldData));
    }
}
